package com.example.seed.service;

import jakarta.annotation.Resource;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class RedissonService {

    @Resource
    private RedissonClient redissonClient;

    private RBucket<Object> getBucket(String key) {
        return redissonClient.getBucket(key);
    }


    /**
     * set 对象
     *
     * @param key    钥匙
     * @param object 对象
     */
    public void setObject(String key, Object object) {
        RBucket<Object> bucket = getBucket(key);
        bucket.set(object);
    }

    public void setObject(String key, Object object, Duration duration) {
        RBucket<Object> bucket = getBucket(key);
        bucket.set(object, duration);
    }

    /**
     * Get 对象
     *
     * @param key 钥匙
     * @return {@link Object}
     */
    public Object getObject(String key) {
        RBucket<Object> bucket = getBucket(key);
        return bucket.get();
    }

    /**
     * 删除
     *
     * @param key 钥匙
     */
    public void delete(String key) {
        RBucket<Object> bucket = getBucket(key);
        bucket.delete();
    }
}
